<template>
  <ContentCard>
    <template #header>
      <pageHeaderNew title="安全设置" showBack></pageHeaderNew>
    </template>
    <main>
      <a-form style="margin: 0 auto; width: 50%" :model="form">
        <a-form-item show-colon field="type" label="是否启用水印">
          <a-switch
            @change="changeType"
            :checked-value="1"
            :unchecked-value="2"
            v-model="form.type"
          />
          <span
            style="
              color: #949494;
              transform: translate(10px, 6px);
              font-size: 12px;
            "
          >
            在页面上开启水印显示，以防止截屏泄密。水印范围：HR网页端、员工端。
          </span>
        </a-form-item>
        <a-form-item show-colon field="post" label="水印内容">
          <a-select @change="change" v-model="form.content" multiple>
            <a-option :value="1" disabled>姓名</a-option>
            <a-option :value="2" disabled>手机后四位号码</a-option>
            <a-option :value="3">部门</a-option>
            <a-option :value="4">职位</a-option>
            <a-option :value="5">时间</a-option>
          </a-select>
        </a-form-item>
        <a-form-item show-colon field="isRead" label="水印样式">
          <div class="watermark">
            <a-watermark :content="content" :alpha="1">
              <div class="content"></div>
            </a-watermark>
          </div>
        </a-form-item>
      </a-form>
    </main>
    <footer>
      <a-button
        class="btn"
        style="
          margin-right: 20px;
          border: 1px rgba(242, 242, 242, 1) solid;
          background-color: #fff;
        "
        @click="$router.go(-1)"
      >
        返回
      </a-button>
      <a-button class="btn" type="primary" @click="save">保存</a-button>
    </footer>
  </ContentCard>
</template>

<script lang="ts" setup>
import { GetStaffDetails } from '@/apis/management/personnel/baseSetting/TransactionSetting';
import { Message } from '@arco-design/web-vue';
import dayjs from 'dayjs';

const router = useRouter();

const { mobile, name, id } = JSON.parse(localStorage.getItem('userInfo') + '');
let department = '';
let position = '';

const form = reactive({ type: 1, content: [1, 2] });
onMounted(async () => {
  const watermarkData = JSON.parse(localStorage.getItem('watermarkData') + '');
  Object.assign(form, watermarkData);
  const code = [
    'CED44AC9966904CD1AB546D309903727C',
    'C7341027925C943708F3AEF98703ECC47'
  ];
  const { ctlValue } = await GetStaffDetails({ id: Number(id) });
  const arrCode = ctlValue.filter(({ ctlCode }: any) => code.includes(ctlCode));
  department =
    arrCode[arrCode.findIndex(({ ctlCode }: any) => ctlCode === code[0])]
      ?.ctlValue || '暂无部门';
  position =
    arrCode[arrCode.findIndex(({ ctlCode }: any) => ctlCode === code[1])]
      ?.ctlValue || '暂无职位';
  changeType(form.type);
});

const save = () => {
  //
  localStorage.setItem('watermarkData', JSON.stringify({ ...form }));
  Message.info('保存成功');
  router.back();
};
const changeType = (e: any) => {
  if (e === 1) {
    changeContent();
  } else {
    content.value = '';
  }
};
const changeContent = async () => {
  content.value = [
    name +
      (mobile?.slice(mobile.length - 4)
        ? mobile.slice(mobile.length - 4)
        : '0000')
  ];
  if (form.content.includes(3) && form.content.includes(4)) {
    content.value.push(department + position);
  } else if (form.content.includes(3)) {
    content.value.push(department);
  } else if (form.content.includes(4)) {
    content.value.push(position);
  }
  if (form.content.includes(5)) {
    content.value.push(dayjs(new Date()).format('YYYY-MM-DD'));
  }
};

const content = ref<string[] | string>([
  name +
    (mobile?.slice(mobile.length - 4)
      ? mobile.slice(mobile.length - 4)
      : '0000')
]);

const change = () => {
  if (form.type === 1) {
    changeContent();
  }
};
</script>

<style scoped lang="less">
.container {
  display: flex;
  gap: 20px;
}
main {
  margin-bottom: 60px;
  overflow: hidden;

  header {
  }
  header h2 {
    display: flex;
    align-items: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #f2f2f2;
  }
  header h2::before {
    content: '';
    display: inline-block;
    width: 7px;
    height: 30px;
    background-color: #165dff;
    margin-right: 10px;
  }
  .custom-radio-card {
    padding: 10px 16px;
    border: 1px solid var(--color-border-2);
    margin-bottom: 10px;
    border-radius: 4px;
    width: 100px;
    height: 50px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .custom-radio-card-mask {
    height: 14px;
    width: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 100%;
    border: 1px solid var(--color-border-2);
    box-sizing: border-box;
  }

  .custom-radio-card-mask-dot {
    width: 8px;
    height: 8px;
    border-radius: 100%;
  }

  .custom-radio-card-title {
    color: var(--color-text-1);
    font-size: 14px;
    font-weight: bold;
    // margin-bottom: 8px;
  }

  .custom-radio-card:hover,
  .custom-radio-card-checked,
  .custom-radio-card:hover .custom-radio-card-mask,
  .custom-radio-card-checked .custom-radio-card-mask {
    border-color: rgb(var(--primary-6));
  }

  .custom-radio-card:hover .custom-radio-card-title,
  .custom-radio-card-checked .custom-radio-card-title,
  .custom-radio-card-checked .custom-radio-card-tips {
    color: rgb(var(--primary-6));
  }

  .custom-radio-card-checked .custom-radio-card-mask-dot {
    background-color: rgb(var(--primary-6));
  }

  .row-box {
    box-sizing: content-box;
    width: 800px;
    height: 255px;
    box-shadow: 1px 1px 5px rgba(153, 153, 153, 0.349019607843137);
    border-radius: 8px;

    &-header {
      height: 49px;
      background-color: rgba(224, 236, 255, 0.188235294117647);
      display: flex;
      align-items: center;
      div {
        position: relative;
        left: 15px;
      }
    }

    &-content {
      height: calc(100% - 49px);
      padding: 10px 20px;
      display: flex;
      align-items: center;
    }
  }
}
footer {
  height: 60px;
  width: calc(100% - 100px);
  background-color: #fff;
  box-shadow: 1px -1px 10px 0 #cacaca;
  position: fixed;
  bottom: 0;
  left: 10%;
  display: flex;
  justify-content: center;
  align-items: center;

  .btn {
    width: 120px;
    height: 40px;
    border-radius: 5px;
  }
}
.watermark {
  width: 100%;
  height: 260px;
  border: 1px solid #3d7eff;
  border-radius: 5px;
  padding: 10px;
  .content {
    background-color: #f2f2f2;
    width: 100%;
    height: 260px;
  }
}
</style>
